package com.zh.onlineexam.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zh.onlineexam.dto.TInfoTextContentDto;
import com.zh.onlineexam.entity.TInfoTextContent;
import com.zh.onlineexam.entity.TSubject;
import com.zh.onlineexam.entity.User;
import com.zh.onlineexam.listener.TInfoTextContentReadListener;
import com.zh.onlineexam.mapper.TInfoTextContentMapper;
import com.zh.onlineexam.service.TInfoTextContentService;
import com.zh.onlineexam.service.TSubjectService;
import com.zh.onlineexam.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @className TInfoTextContentServiceImpl
 * @Description TODO
 * @Auther 29934
 * @Date 2022/8/20 10:40
 * @Version 1.0
 **/
@Service
@RequiredArgsConstructor
@Slf4j
public class TInfoTextContentServiceImpl extends ServiceImpl<TInfoTextContentMapper, TInfoTextContent> implements TInfoTextContentService {

    private final UserService userService;
    private final TSubjectService subjectService;

    @Override
    public boolean saveImportData() {
        List<TInfoTextContentDto> list = TInfoTextContentReadListener.gettInfoTextContentList();
        List<TInfoTextContent> tInfoTextContentList = new ArrayList<>();
        User user = userService.getOne(new QueryWrapper<User>().eq("id", StpUtil.getLoginId()));
        list.forEach( a -> {
            a.setCreateUser(user.getUserName());
            //获取a中的年级名(levelName)对应t_subject表中的level_name
            String levelName = a.getLevelName();
            //获取a中的学科名(subjectName)对应t_subject表中的name
            String subjectName = a.getSubjectName();
            TSubject one = subjectService.getOne(new QueryWrapper<TSubject>()
                    .eq("level_name", levelName)
                    .eq("name", subjectName));
            //问题类型
            Integer questionType =  "单选题".equals(a.getQuestionType()) ? 1
                    : "多选题".equals(a.getQuestionType()) ? 2
                    : "判断题".equals(a.getQuestionType()) ? 3
                    : "填空题".equals(a.getQuestionType()) ? 4
                    : "简答题".equals(a.getQuestionType()) ? 5 : 6;
            if(!ObjectUtil.isEmpty(one)){
                TInfoTextContent content = new TInfoTextContent();
                BeanUtils.copyProperties(a, content);
                content.setQuestionType(questionType);
                content.setSubjectId(one.getId());
                content.setLevel(one.getLevel());
                tInfoTextContentList.add(content);
            }else{
                return ;
            }
        });
        boolean b = this.saveBatch(tInfoTextContentList);
        TInfoTextContentReadListener.clearTInfoTextContentList();
        return b;
    }
}
